﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace LINQDemo1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string[] names = { "John", "Peter", "Joe", "Patrick", "Donald", "Eric" };

            IEnumerable<string> namesWithFiveCharacters =
                                        from name in names
                                        where name.Length < 5
                                        select name;            

            /* Krísuvíkurleiðin
            lstResults.Items.Clear();
            foreach(var name in namesWithFiveCharacters)
                lstResults.Items.Add(name);
             */

            // Eða einfaldlega
            lstResults.DataSource = namesWithFiveCharacters.ToList();

        }

        private void btnLoopThroughObjects_Click(object sender, EventArgs e)
        {
            List<Person> persons = new List<Person>
                {
                    new Person{FirstName = "Joe", LastName = "Adams", Address = "Chandler"},
                    new Person{FirstName = "Don", LastName ="Alexander", Address = "Washington"},
                    new Person{FirstName = "Dave", LastName = "Ashton", Address = "Seattle"},
                    new Person{FirstName = "Bill", LastName = "Pierce", Address = "Sacromento"},
                    new Person{FirstName = "Bill", LastName ="Giard", Address = "Camphill"}
                };

            var personsNotInSeattle = from person in persons // Skilgreining
                                      where person.Address != "Seattle" // Skilyrði
                                      orderby person.FirstName // Röðun
                                      select person; // Val á staki úr lista
            
            // Viljum sérsmíðað útlit á strengina hér. Notum krísuvíkurleiðina.
            lstResults.DataSource = null;
            lstResults.Items.Clear();

            // Núna loksins keyrist fyrirspurnin
            foreach (var person in personsNotInSeattle)
            {
                lstResults.Items.Add(person.FirstName + " " + person.LastName +
                    " - " + person.Address);
            }
        }
    }
} 

